package cn.water.luban.demo.order.main.controller;



import cn.water.demo.luban.commons.restful.JsonResult;
import cn.water.demo.luban.commons.restful.ResponseCode;
import cn.water.demo.luban.protocol.order.param.OrderAddParam;
import cn.water.luban.demo.order.main.handler.FallbackHandler;
import cn.water.luban.demo.order.main.handler.OrderHandler;
import cn.water.luban.demo.order.main.service.OrderService;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/base/order")
@Api(tags = "订单")
public class OrderController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @PostMapping("/add")
    @ApiOperation("新增订单的功能")
    @SentinelResource(value = "addOrder",blockHandler = "orderHandler",blockHandlerClass = OrderHandler.class,
                      fallback = "orderFallBack",fallbackClass = FallbackHandler.class)
    public JsonResult addOrder(OrderAddParam orderAddParam){
        //sentinel限流
        Message<OrderAddParam> message = MessageBuilder.withPayload(orderAddParam).build();
        rocketMQTemplate.asyncSend("luban-order-topic", message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("新增订单消息发送成功");
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("新增订单消息发送失败"+throwable.getMessage());
            }
        });
        System.out.println("新增订单消息已发送");
        return JsonResult.ok("新增订单完成!");
    }

}
